/**
 * jims
 */
package com.jims.api.v1.controller.common;


import com.baomidou.kisso.my.controller.BaseController;
import com.jims.master.common.data.Message;
import com.jims.master.common.utils.SecurityUtil;
import com.jims.master.modules.sys.entity.UserAddress;
import com.jims.master.modules.sys.service.UserAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 地址表Controller
 * @author jct
 * @version 2017-12-05
 */
@RestController
@RequestMapping(value = "userAddress")
public class UserAddressController extends BaseController {

	@Autowired
	private UserAddressService userAddressService;

	/**
	 * @Title:
	 * @Description: （根据用户id查找地址）
	 * @param userId  用户id
	 * @return
	 * @author jict
	 * @date 2017/12/5
	 * @throws
	 */
	@RequestMapping(value = "V100/getUserAddress",method ={RequestMethod.GET,RequestMethod.POST})
	public String getUserAddress(@RequestParam(required=true) String userId) {
		Message msg=new Message();
		List<UserAddress> list = userAddressService.getUserAddress(userId);
		msg.setData(list);
		return SecurityUtil.encryptMsgData(msg);
	}

	/**
	 * @Title:
	 * @Description: （新增用户地址）
	 * @param userId  用户id
	 * @param address  详细地址
	 * @param area  区域
	 * @param phone  手机号
	 * @param name  姓名
	 * @param isdefault  是否为默认地址
	 * @return
	 * @author jict
	 * @date 2017/12/5
	 * @throws
	 */
	@RequestMapping(value = "V100/saveUserAddress",method ={RequestMethod.GET,RequestMethod.POST})
	public String saveUserAddress(@RequestParam(required=true) String userId,@RequestParam(required=true) String name,@RequestParam(required=true) String area,@RequestParam(required=true) String address,@RequestParam(required=true) String phone,@RequestParam(required=true) String isdefault) {
		Message msg=new Message();
		if("1".equals(isdefault)){
			UserAddress defaultAddress = userAddressService.getDefaultAddress(userId);
			if (defaultAddress!=null) {
				defaultAddress.setIsdefault("0");
				userAddressService.save(defaultAddress);
			}
		}
		UserAddress userAddress = new UserAddress();
		userAddress.setUserid(userId);
		userAddress.setName(name);
		userAddress.setAddress(address);
		userAddress.setArea(area);
		userAddress.setPhone(phone);
		userAddress.setIsdefault(isdefault);
		int result = userAddressService.save(userAddress);
		if(result == 1){
			msg.setData("保存成功");
			return SecurityUtil.encryptMsgData(msg);
		}else{
			msg.setData("保存失败");
			return SecurityUtil.encryptMsgData(msg);
		}
	}


	/**
	 * @Title:
	 * @Description: （修改用户地址）
	 * @param addressId  地址id
	 * @param address  详细地址
	 * @param area  区域
	 * @param phone  手机号
	 * @param name  姓名
	 * @param isdefault  是否为默认地址
	 * @return
	 * @author jict
	 * @date 2017/12/5
	 * @throws
	 */
	@RequestMapping(value = "V100/updateUserAddress",method ={RequestMethod.GET,RequestMethod.POST})
	public String updateUserAddress(@RequestParam(required=true) String addressId,@RequestParam(required=true) String userId, String name,String area,String address,String phone,String isdefault) {
		Message msg=new Message();
		if("1".equals(isdefault)){
			UserAddress defaultAddress = userAddressService.getDefaultAddress(userId);
			if (defaultAddress!=null) {
				defaultAddress.setIsdefault("0");
				userAddressService.save(defaultAddress);
			}
		}
		UserAddress userAddress = userAddressService.get(addressId);
		if (name!=null&&name!="") {
			userAddress.setName(name);
		}
		if (address!=null&&address!="") {
			userAddress.setAddress(address);
		}
		if (area!=null&&area!="") {
			userAddress.setArea(area);
		}
		if (phone!=null&&phone!="") {
			userAddress.setPhone(phone);
		}
		userAddress.setIsdefault(isdefault);
		int result = userAddressService.save(userAddress);
		if(result == 1){
			msg.setData("修改成功");
			return SecurityUtil.encryptMsgData(msg);
		}else{
			msg.setData("修改失败");
			return SecurityUtil.encryptMsgData(msg);
		}
	}

	/**
	 * @Title:
	 * @Description: （根据地址id删除地址）
	 * @param addressId  地址id
	 * @return
	 * @author jict
	 * @date 2017/12/5
	 * @throws
	 */
	@RequestMapping(value = "V100/deleteUserAddress",method ={RequestMethod.GET,RequestMethod.POST})
	public String deleteUserAddress(@RequestParam(required=true) String addressId) {
		Message msg=new Message();
		UserAddress userAddress = userAddressService.get(addressId);
		int result = userAddressService.delete(userAddress);
		if(result == 1){
			msg.setData("删除成功");
			return SecurityUtil.encryptMsgData(msg);
		}else{
			msg.setData("删除失败");
			return SecurityUtil.encryptMsgData(msg);
		}
	}

	/**
	 * @Title:
	 * @Description: （根据地址id获取地址）
	 * @param addressId  地址id
	 * @return
	 * @author jict
	 * @date 2017/12/5
	 * @throws
	 */
	@RequestMapping(value = "V100/getUserAddressById", method = {RequestMethod.GET, RequestMethod.POST})
	public String getUserAddressById(@RequestParam(required = true) String addressId) {
		Message msg = new Message();
		UserAddress userAddress = userAddressService.get(addressId);
		msg.setData(userAddress);
		return SecurityUtil.encryptMsgData(msg);
	}

}